<template>
  <div class='quickReply'>
    <el-dialog :visible.sync="isShow" @close="closed" center ref="quickReplyDialog" class="quickReplyDialog" @opened='opened'>
      <div slot="title" class="dialog-title">
        <span class="dialog-title-text">审核订单</span>
        <span class="dialog-title-hint">{{ orderForm.order_number }}</span>
        <span class="dialog-title-hint">{{ orderForm.patent_name}}</span>
      </div>
      <!-- 代理人变更记录 -->
      <div  v-if='orderForm.update_records && orderForm.update_records.length' class='updateAgent'>
        <h4 class='mb10'>代理人变更记录</h4>
        <p v-for="(item,index) in getUpdateAgent" :key='index' class='tc'>
          <span class="agentName oldAgent"> {{ `${item.old}（${item.old_num}）` }} </span><span class="agentSeparated">→</span><span class="agentName green"> {{ `${item.new}（${item.new_num}）` }} </span>
        </p>
      </div>
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in opinions"
          :key="index"
          :timestamp="item.created_at"
        >
          <el-card class="messagebox">
            <Tooltip content="点我回复" class="fr " >
              <i class="el-icon-chat-dot-round leaveWord ml5" @click="showTextarea(item, index)"></i>
            </Tooltip>
            <!-- <div class="item">
              <span class="item_time">{{item.created_at}}</span>
              <span class="item_user">审核员</span>
              <div class="item_message messageBackground_l">
                <span>{{item.review_cn}}</span>
              </div>
              <div class="deduction_message messageBackground_l" v-if="item.deduction_reason != ''">
                <div>
                  <span>扣款原因：</span>
                  <span class="ml10 font_danger">{{item.deduction_reason}}</span>
                </div>
                <div>
                  <span>扣款金额：</span>
                  <span class="ml10 font_danger">{{item.money_cn}}</span>
                </div>
              </div>
            </div> -->
            <div class="agent_item clearfloat" v-if="item.role_id == '2'">
              <span class="item_user fr">代理人</span>
              <span class="item_time fr">{{item.created_at}}</span>
              <div class="item_message messageBackground_r fr">
                <span>{{item.review_cn}}</span>
              </div>
            </div>
            <div v-else class="item">
              <span class="item_time">{{item.created_at}}</span>
              <span class="item_user">审核员</span>
              <div class="item_message messageBackground_l">
                <span>{{item.review_cn}}</span>
              </div>
              <div class="deduction_message messageBackground_l" v-if="item.deduction_reason != ''">
                <div>
                  <span>扣款原因：</span>
                  <span class="ml10 font_danger">{{item.deduction_reason}}</span>
                </div>
                <div>
                  <span>扣款金额：</span>
                  <span class="ml10 font_danger">{{item.money_cn}}</span>
                </div>
              </div>
            </div>
            <div v-for="(agent, index2) in item.agent_message" :key="index2">
              <div class="agent_item clearfloat" v-if="agent.role_id == '2'">
                <span class="item_user fr">代理人</span>
                <span class="item_time fr">{{agent.created_at}}</span>
                <div class="item_message messageBackground_r fr">
                  <span>{{agent.review_cn}}</span>
                </div>
              </div>
              <div v-else class="item">
                <span class="item_time">{{agent.created_at}}</span>
                <span class="item_user">审核员</span>
                <div class="item_message messageBackground_l">
                  <span>{{agent.review_cn}}</span>
                </div>
              </div>
            </div>
            <div v-if="item.show_input" class="clearfloat mt20">
              <el-input type="textarea" :rows="4" placeholder="请输入内容" v-model="replyMessage"></el-input>
              <el-button class="fr mt10" type="primary" @click="submit(item)">发送</el-button>
              <!-- <el-button class="fr mt10 mr10" @click="resets ">重置</el-button> -->
            </div>
          </el-card>
        </el-timeline-item>
      </el-timeline>
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="审核意见" prop="review">
          <el-input type="textarea" :rows="4" placeholder="请输入审核意见" v-model="form.review"></el-input>
        </el-form-item>
      </el-form>
      <div class='btns'>
        <el-button
          type="primary"
          class="mr10"
          @click="submitForm('ruleForm','yes')"
          :disabled="orderForm.audit_op_time"
          v-no-more-click:3000
        >通 过</el-button>
        <el-dropdown trigger="click" placement="bottom" class="free-drop">
          <el-button type="danger">退 回</el-button>
          <el-dropdown-menu slot="dropdown" class="free-drop-menu">
            <el-dropdown-item class="mb10">
              <el-button
                type="warning"
                @click="submitForm('ruleForm','no')"
                v-no-more-click:3000
              >普通退回</el-button>
            </el-dropdown-item>
            <el-dropdown-item class="mb10">
              <el-button
                type="danger"
                @click="deduction(orderForm, 'ruleForm')"
                v-no-more-click:3000
              >扣款退回</el-button>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>

    </el-dialog>
    <deductionDialog ref="deductionDialog" @refresh='refresh'/>
  </div>
</template>

<script>
import { doAuditor } from "~/api/auditor";
// 扣款
import deductionDialog from "./deduction-dialog";
export default {
  components: {
    deductionDialog
  },
  data() {
    return {
      isShow: false,
      orderForm: {},
      form: {
        review: ''
      },
      rules: {
        review: [
          {
            // required: true,
            message: "请输入审批意见",
            trigger: "blur"
          }
        ]
      },
      opinions: [],
      order: null,
      replyMessage: "",
      getUpdateAgent: []
    };
  },
  watch:{
    orderForm: {
      handler: function(val) {
        if (val.update_records) {

          for (let i = 0; i < val.update_records.length; i++ ) {
            if (JSON.parse(val.update_records[i].review) instanceof Object) {
              let item = JSON.parse(val.update_records[i].review)
              this.getUpdateAgent.push(item)
            }
          }
        }
      },
      deep: true
    }
  },
  computed: {

  },
  methods: {

    show(data) {
      this.order = data.id
      this.orderForm = data;
      this.opinions = data.opinions
      this.isShow = true
      console.log(this.orderForm)
    },
    opened() {
      // 滚动条置底
      this.scrollEnd()
    },
    scrollEnd(){
      this.$nextTick(() => {
        const dialogViewport = document.querySelector('.quickReply').childNodes[0].childNodes[0]
        const scrollHeight = dialogViewport.scrollHeight
        dialogViewport.scrollTop  = scrollHeight
      })
    },
    closed() {
      this.replyMessage = ""
      this.$refs["ruleForm"].resetFields();
    },
    refresh() {
      let that = this
      setTimeout(() => {
        that.isShow = false
      }, 500)
      this.$emit("refresh")
    },
     //确认通过 / 未通过
    submitForm(formName, pass) {
      let that = this;
      that.$refs[formName].validate(valid => {
        if (valid) {

          let obj = {
            order: that.order,
            auditor: that.$store.state.user.id,
            pass: pass,
            review: that.form.review
          };
          if (pass == 'yes') {
            if (that.form.review == '') {
              obj.review = '过'
            }
          } else {
            if (that.form.review == '') {
              this.$message({
                type: 'warning',
                message: '请输入审核意见'
              })
              return
            }
          }

          doAuditor(that.$axios, obj)
            .then(({ data }) => {
              if (data.status == "success") {
                that.$message.success("操作成功")
                this.isShow = false
                this.$emit("refresh")
              } else {
                that.$message.error(data.message);
              }
            })
            .catch(err => {
              that.$message.error(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    deduction(data, formName) {
      // this.$refs[formName].validate(valid => {
      //   if (valid) {
      //   }
      // });
      if (this.form.review == '') {
        this.$message({
          type: 'warning',
          message: '请输入审核意见'
        })

      } else {
        this.$refs["deductionDialog"].show(data, this.form.review);
      }
    },
    submit(item) {
      if (this.replyMessage == "") {
        this.$message({
          message: "请输入消息",
          type: "warning"
        });
      } else {
        this.submitPost(item)
      }
    },
    showTextarea(item, index) {
      for(let i = 0; i < this.opinions.length; i++) {
        this.opinions[i].show_input = false;
        if (i == index) {
          this.opinions[i].show_input = true
          this.replyMessage = ""
        }
      }
    },
    submitPost(data) {
      let params = {
        opinion_id: data.id,
        review: this.replyMessage,
        type: 'auditor'
      }
      this.$axios
        .post(`/api/order/${data.order_id}/agent_message`, params)
        .then(res => {
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "回复成功！"
            });
            this.isShow = false;
            this.$emit("refresh");
          } else {
            this.$message({
              type: "error",
              message: res.data.message
            });
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
  }
};
</script>

<style lang='less' type='text/css'>
.quickReply {
  .el-dialog {
    width: 900px;
    max-height: 800px;
    overflow-y: auto;
    margin-top: 5vh !important;

    .updateAgent {
      padding: 10px;
      border: 1px solid #E4E7ED;
      border-radius: 5px;
      margin-left: 28px;
    }
  }
  .el-form {
    padding-left: 28px;

  }
  .btns {
    text-align: center;
    padding: 10px;
    margin-bottom:15px;
  }
  .messagebox {
    width: 100%;
    background: #eef1f3;
  }

  .messageBackground_l {
    background: #fff;
  }
  .messageBackground_r {
    background: #c9e7ff;
  }
  .item {
    width: 100%;
    margin-bottom: 10px;
    .item_user {
      display: inline-block;
      width: 60px;
      font-size: 16px;
      font-weight: bold;
    }
    .item_message {
      max-width: calc(100% - 170px);
      display: inline-block;
      padding: 10px 15px;
      box-sizing: border-box;
      margin-left: 15px;
      border-radius: 5px;
      vertical-align: top;
      // margin-right: 90px;
    }
    .item_time {
      font-size: 12px;
      display: block;
      padding-left: 80px;
      margin-bottom: 5px;
      color: #ccc;
    }
    .deduction_message {
      margin: 20px 0 0 80px;
      max-width: calc(100% - 170px);
      padding: 10px 15px;
      border-radius: 5px;
    }
  }
  .agent_item {
    width: 100%;
    margin-bottom: 10px;
    position: relative;
    box-sizing: border-box;
    padding-top: 20px;
    .item_user {
      display: inline-block;
      width: 60px;
      font-size: 16px;
      font-weight: bold;
    }
    .item_time {
      position: absolute;
      right: 95px;
      top: 0px;
      font-size: 12px;
      display: block;
      // padding-right: 80px;
      margin-bottom: 5px;
      color: #ccc;
    }
    .item_message {
      max-width: calc(100% - 170px);
      display: inline-block;
      padding: 10px 15px;
      box-sizing: border-box;
      margin-right: 30px;
      border-radius: 5px;
      vertical-align: top;
      // margin-left: 80px;
    }
  }
  .ivu-tooltip {
    position: absolute;
    right: 20px;
    top: 10px;
    z-index: 2000;
  }
}
.free-drop-menu {
  .el-dropdown-menu__item {
    padding: 0 10px;
    line-height: 0;
    .normal-btn {
      width: 90px;
      height: 26px;
      padding: 0;
      span {
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
}

</style>
